package design.pattern.Lu05BuilderPattern;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @program JavaBase
 * @description: 建造者模式
 * @author: zhanglu
 * @create: 2020-03-22 19:53:00
 */
public class Lu05BuilderPattern {

    public static void main(String[] args) {

        List<String> sequences = Arrays.asList("start", "engineBoom", "stop");

        //普通方式
        ICar iCar = new Car02();
        iCar.setSequences(sequences);
        iCar.run();

        //建造者模式
        CarBuilder carBuilder = new Car02Builder();
        carBuilder.setSequence(sequences);
        carBuilder.getCar().run();

    }

}


abstract class ICar{

    private List<String> sequences = new ArrayList<>();

    //车名
    abstract void name();
    //启动
    abstract void start();
    //引擎
    abstract void engineBoom();
    //喇叭
    abstract void alarm();
    //停止
    abstract void stop();

    protected final void setSequences(List<String> sequences){
        this.sequences = sequences;
    }

    protected final void run(){

        this.name();

        for (String s : sequences){
            if(s.equalsIgnoreCase("start")){
                this.start();
            }else if(s.equalsIgnoreCase("engineBoom")){
                this.engineBoom();
            }else if(s.equalsIgnoreCase("alarm")){
                this.alarm();
            }else if(s.equalsIgnoreCase("stop")){
                this.stop();
            }
        }

    }

}

class Car02 extends ICar{

    @Override
    void name() {
        System.out.println("car02");
    }

    @Override
    void start() {
        System.out.println("start");
    }

    @Override
    void engineBoom() {
        System.out.println("engineBoom");
    }

    @Override
    void alarm() {
        System.out.println("alarm");
    }

    @Override
    void stop() {
        System.out.println("stop");
    }

}

abstract class CarBuilder{

    abstract void setSequence(List<String> sequence);

    abstract ICar getCar();

}

class Car02Builder extends CarBuilder{

    private ICar car = new Car02();

    @Override
    void setSequence(List<String> sequence) {
        this.car.setSequences(sequence);
    }

    @Override
    ICar getCar() {
        return this.car;
    }
}





